console.log('腾讯新闻脚本注入，与网页环境一致(MAIN),修改XMLHttpRequest');
console.log(localStorage.getItem('chrome_test'));
(function (xhr){
  if (XMLHttpRequest.prototype.hasChange) return;
  var XHR = XMLHttpRequest.prototype;
  XHR.hasChange = true;
  var { open, send } = XHR;
  XHR.open = function (method, url){
    this._method = method;
    this._url = url;
    return open.apply(this, arguments);
  };
  XHR.send = function (){
    const filterUrls = ['/proxyhttp'];
    let filterRes = filterUrls.filter(url => this._url.includes(url)).length > 0 ? true : false;
    if (filterRes){
      this.addEventListener('load', function (xhr){
        // console.log(this._url);
        // console.log('xhr', xhr);
        // console.log(JSON.parse(xhr.target.response))
        let res = JSON.parse(xhr.target.response);
        if (res.vinfo){
          let videoInfo = JSON.parse(res.vinfo);
          /*
            vinfo.vl.vi[0]: {
              // 对应dom里.video-wrap下的div._mod_thumbplayer_container_#video-0-vid
              vid: "t1137y8jacl",
              fn: "szg_4279_50001_0b5324akyaaafeakjli4pbufdv6dvtlqblca.f306313.ts"
              ul: {
                m3u8: "",
                ui: [{
                  url: "https://a0cba6edd14520403d82fe44b7835b89.v.smtcdns.com/omts.tc.qq.com/A7IEc4JADX8HE5KqqaPDDnjpsmItilM1VGIm_YB-6X0U/B_3k--xdVBUHYl1q0K2jODe3dl54iT4sMiBjvk2oMeBYMnH2q4sOInNX8PBn4lXH1Y13kWLASdASwwk6mq9wfVPQNUo4nF-BnkxozXYs1BSP2AxSgvc13ueNdW2GBfzeLJF44hqZdhTmmaPiOFUhmurw/svp_50001/sZEaVtdqnUTepnPalG4NJNfFc9_WvAsNI4NuXt0mvHdeJN976TqIiDfV2FHpoPS1hspzCQInHrPfDsxxUHwE0sdV__0wgTWoXPxMkKc-z9qIdGV-GR3eX8FJ2tyOo-wISBYzYtkUleiiR0Pj7lpoJJ1a6SL2pXDJE2qLbFsorgSe8yHDvOHiC_fl12x6PQK9vKPHuWOWRCV2WP9tswuAm6hvY1Guth-p/szg_4279_50001_0b5324akyaaafeakjli4pbufdv6dvtlqblca.f306313.ts.m3u8?ver=4"
                }]
              }
            }
          */
          videoInfo = videoInfo?.vl?.vi || [];
          videoInfo = videoInfo[0];
          console.log(videoInfo)
          // 要从完整路径里去除的尾部查找部分
          let urlSliceComponent = videoInfo.fn;
          // 视频唯一标识
          let vid = videoInfo.vid;
          let vtitle = videoInfo.ti;
          // console.log(urlSliceComponent);
          let videoTsNames = deelVideoList(videoInfo.ul.m3u8);
          // 完整请求地址(非视频)
          let completeUrl = videoInfo.ul.ui[0].url;
          // 截取后可以拼接视频url的前面部分
          let videoUrlHead = completeUrl.slice(0, completeUrl.lastIndexOf(urlSliceComponent));
          let videoUrls = videoTsNames.map(videoName => `${videoUrlHead}${videoName}`);
          localStorage.setItem(`ce-${vid}`, JSON.stringify({
            title: vtitle,
            urls: videoUrls
          }));
        }
      })
    }
    return send.apply(this, arguments);
  };
})(XMLHttpRequest);

function deelVideoList(m3u8){
  let reg = /(?<=\s).*?(?=#)/gs;
  let regFilters = m3u8.match(reg);
  if (regFilters){
    return regFilters.filter(url => url.trim() !== url);
  }
}

// function injectData(){
//   let a = document.createElement('a');
//   a.dataset.name = 'test';
//   a.dataset.blobUrl = '11';

//   a.style.display = 'none';
//   a.innerHTML = '测试用的a标签';
//   a.className = 'chrome-test-link'
//   document.body.appendChild(a);
// }

